<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">基本信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="设备名称" v-bind="validateInfos.devName">
            <a-input v-model:value="formData.devName" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="自编号" v-bind="validateInfos.devNumber">
            <a-input v-model:value="formData.devNumber" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="类型编码" v-bind="validateInfos.devTypeCode">
            <a-input v-model:value="formData.devTypeCode" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规格型号" v-bind="validateInfos.devModel">
            <a-input v-model:value="formData.devModel" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="设备分类" v-bind="validateInfos.devType">
            <j-dict-select-tag v-model:value="formData.devType" dictCode="dev_type" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="设备状态" v-bind="validateInfos.status">
            <j-dict-select-tag v-model:value="formData.status" dictCode="dev_status" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
      <a-col :span="8">
          <a-form-item label="公用标识" v-bind="validateInfos.commonMark">
            <j-dict-select-tag v-model:value="formData.commonMark" dictCode="yn" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
      <a-col :span="8">
        <a-form-item label="技术参数" v-bind="validateInfos.params">
          <a-input v-model:value="formData.params" :disabled="disabled"
            style="width: 520px; border-radius: 4px 4px 4px 4px;  opacity: 1;  border: 0.5px solid #DDDDDD;"></a-input>
        </a-form-item>
      </a-col>
    </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="班组" v-bind="validateInfos.team">
            <j-dict-select-tag v-model:value="formData.team" dictCode="team" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="保管人" v-bind="validateInfos.custodian">
            <a-input v-model:value="formData.custodian" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="ABC标识" v-bind="validateInfos.abcde">
            <j-dict-select-tag v-model:value="formData.abcde" dictCode="ABCDE" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="CMA设备" v-bind="validateInfos.cma">
            <j-dict-select-tag v-model:value="formData.cma" dictCode="yn" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="重点设备" v-bind="validateInfos.important">
            <j-dict-select-tag v-model:value="formData.important" dictCode="yn" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="使用状态" v-bind="validateInfos.useStatus">
            <j-dict-select-tag v-model:value="formData.useStatus" dictCode="use_status" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="备注" v-bind="validateInfos.remarks">
            <a-input v-model:value="formData.remarks"
              style="width: 840px; border-radius: 4px 4px 4px 4px;  opacity: 1;  border: 0.5px solid #DDDDDD;"
              :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">生产信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="制造商" v-bind="validateInfos.producer">
            <a-input v-model:value="formData.producer" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="国别" v-bind="validateInfos.country">
            <j-dict-select-tag v-model:value="formData.country" dictCode="country" :show-choose-option="false"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="出厂编号" v-bind="validateInfos.initialNumber">
            <a-input v-model:value="formData.initialNumber" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="制造年月" v-bind="validateInfos.produceDate">
            <a-date-picker v-model:value="formData.produceDate" value-format="YYYY-MM-DD" style="width: 100%"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="效用年限" v-bind="validateInfos.validity">
            <a-input-number v-model:value="formData.validity" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="投产日期" v-bind="validateInfos.operationDate">
            <a-date-picker v-model:value="formData.operationDate" value-format="YYYY-MM-DD" style="width: 100%"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">维保信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="是否需要检定" :labelCol="{ span: 7 }" v-bind="validateInfos.checkMark">
            <j-dict-select-tag v-model:value="formData.checkMark" dictCode="yn" :show-choose-option="false"
              :disabled="disabled" style="width: 172px;" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="维保周期/月" :labelCol="{ span: 6 }" v-bind="validateInfos.checkCycle">
            <a-input-number v-model:value="formData.checkCycle" style="width: 95%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="上次维保时间" :labelCol="{ span: 9 }" v-bind="validateInfos.lastCheckDate">
            <a-date-picker v-model:value="formData.lastCheckDate" value-format="YYYY-MM-DD"
              style="width: 149px !important;" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="定检日期"  v-bind="validateInfos.checkDate">
            <a-date-picker v-model:value="formData.checkDate" value-format="YYYY-MM-DD" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="临检日期"  v-bind="validateInfos.visitDate">
            <a-date-picker v-model:value="formData.visitDate" value-format="YYYY-MM-DD" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">资产信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="资产编号" v-bind="validateInfos.assetNumber">
            <a-input v-model:value="formData.assetNumber" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="所属单位" v-bind="validateInfos.company">
            <a-input v-model:value="formData.company" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="资产原值" v-bind="validateInfos.cost">
            <a-input-number v-model:value="formData.cost" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="累计折旧" v-bind="validateInfos.totalDeprecition">
            <a-input-number v-model:value="formData.totalDeprecition" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="期末净值" v-bind="validateInfos.netWorth">
            <a-input-number v-model:value="formData.netWorth" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="年累折旧" v-bind="validateInfos.yearDeprecition">
            <a-input-number v-model:value="formData.yearDeprecition" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="存放地点" v-bind="validateInfos.savePlace">
            <a-input v-model:value="formData.savePlace" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="设备照片" v-bind="validateInfos.devPicture">
            <j-image-upload v-model:value="formData.devPicture" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规程文件" v-bind="validateInfos.rulesDoc">
            <j-upload v-model:value="formData.rulesDoc" :disabled="disabled"></j-upload>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8"> -->
        <!-- <a-form-item label="二维码" v-bind="validateInfos.qrCode"> -->
        <!-- </a-form-item> -->
        <!-- <a-button type="primary" @click="createQrCode" preIcon="ant-design:plus-outlined"> 生成二维码</a-button>
          <LabDeviceQr @register="registerModal" /> -->
        <!-- </a-col> -->
      </a-row>

    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
//import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
import { getValueType } from '/@/utils';
import { saveOrUpdate } from '../LabDevice.api';
import { Form } from 'ant-design-vue';
//import LabDeviceQr from './LabDeviceQr.vue';
//import { useModal } from '/@/components/Modal';
//import { addCollection } from '@iconify/iconify';
//import { QrCode } from '/@/components/Qrcode/index';
//import { CollapseContainer } from '/@/components/Container/index';


const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => { } },
  formBpm: { type: Boolean, default: true }
});
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const formData = reactive<Record<string, any>>({
  id: '',
  devName: '',
  devNumber: '',
  devTypeCode: '',
  devModel: '',
  params: '',
  status: '',
  producer: '',
  country: '',
  initialNumber: '',
  produceDate: '',
  operationDate: '',
  validity: undefined,
  cost: undefined,
  company: '',
  totalDeprecition: undefined,
  netWorth: undefined,
  yearDeprecition: undefined,
  assetNumber: '',
  savePlace: '',
  custodian: '',
  team: '',
  abcde: '',
  cma: '',
  remarks: '',
  devType: '',
  useStatus: '',
  useTo: '',
  traceWay: '',
  checkParam: '',
  checkSite: '',
  checkCycle: undefined,
  lastCheckDate: '',
  endCheckDate: '',
  planCheckDate: '',
  important: '',
  devPicture: '',
  qrCode: '',
  rulesDoc: '',
  checkMark: '',
  commonMark:'',
  checkDate:'',
  visitDate:''
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = {
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });


// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});

//弹窗注册
//const [registerModal, { openModal}] = useModal();

/**
 * 新增
 */
function add() {
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(() => {
    resetFields();
    //赋值
    Object.assign(formData, record);
  });
}

/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  let model = formData;
  if (model.id) {
    isUpdate.value = true;
  }
  //循环数据
  for (let data in model) {
    //如果该数据是数组并且是字符串类型
    if (model[data] instanceof Array) {
      let valueType = getValueType(formRef.value.getProps, data);
      //如果是字符串类型的需要变成以逗号分割的字符串
      if (valueType === 'string') {
        model[data] = model[data].join(',');
      }
    }
  }
  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

//打开二维码弹窗
// function createQrCode() {
//   openModal(true)
// }

defineExpose({
  add,
  edit,
  submitForm,
  // createQrCode,
  // registerModal,
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  min-height: 500px !important;
  overflow-y: auto;
  padding: 24px 24px 24px 24px;
}


.antd-modal-divider {
  width: 2px;
  height: 16px;
  background: #009688;
  border-radius: 0px 0px 0px 0px;
  opacity: 1;
}

.hClass {
  width: 60px;
  height: 20px;
  font-size: 14px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
  font-weight: 550;
  color: #009688;
  line-height: 14px;
}

.ant-form-item-label {
  width: 60px;
  height: 20px;
  font-size: 14px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
  font-weight: 400;
  color: #727272;
  line-height: 20px;
}

.ant-input {
  width: 200px;
  height: 32px;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  border: 1px solid #DDDDDD;
}

.ant-picker {
  width: 200px !important;
  height: 32px;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  border: 1px solid #DDDDDD;
}


:deep(.ant-form-item-control-input) {
  width: 200px !important;
}
</style>
